<?php
namespace Admin\Controller;
/**
 * 后台json管理
 * @author haiqin
 *
 */
class JsonController extends CommonController{
	
	/**
	 * json列表
	 */
	public function index(){
		$this->display();
	}
	
	/**
	 * @desc 获取日志数据（当前登录用户）
	 */
	public function getdata(){
	
		$p = I('p') ? I('p') : 1;
		$row = I('row') ? I('row') : 10;
	
		$m = M('Jsons');
		$data['data'] = $m->where($map)->page($p,$row)->select();
		//$data['sql'] = $m->_sql();
		$data['page']['total'] = $m->where($map)->count();
		$data['page']['cur'] = $p;
		$data['page']['row'] = $row;
	
		_apiReturn(1, L('success'), $data, $url); //数据获取成功
	}
	
	/**
	 * 获取json
	 */
	public function getjson(){
		if(I('id')){
			$map['id'] = I('id');
		}
		
		if(I('key')){
			$map['key'] = I('key');
		}
		
		if(!$map)
			_apiReturn(0, "miss param id or key");
		
		$data = M('Jsons')->where($map)->find();
		
		if($data){
			// 返回JSON数据格式到客户端 包含状态信息
			header('Content-Type:application/json; charset=utf-8');
			header("Access-Control-Allow-Origin: *");  //跨域请求
			exit(json_decode($data['json']));
		}else{
			_apiReturn(0, 'sorry , can not find any json under this key!');//获取成功
		}
	}
	
	/**
	 * 检查是否已经存在了
	 */
	public function checkexist(){
		if(!I('key')){
			_apiReturn(0, "miss param key");
		}
		
		$map['key'] = I('key');
		
		$tmp = M('Jsons')->where($map)->find();
		if($tmp){
			$data = 1;
		}else{
			$data = 0;
		}
		
		_apiReturn(1, 'success', $data, $url);
	}
	
	/**
	 * json编辑页面
	 */
	public function edit(){
		if($_GET['id']){
			$this->info = M('Jsons')->find($_GET['id']);
		}else{
			$info['json'] = '{}';
			$this->info = $info;
		}
		$this->display();
	}
	
	/**
	 * json编辑post
	 */
	public function edit_post(){
		if(IS_POST){
			$_POST['update_time'] = Date("Y-m-d H:i:s");
			$m = M('Jsons');
			$tmpData = $m->create($_POST);
			if($_POST['id']){
				if($m->save($tmpData)){
					$this->success("update success");
				}else{
					$this->error("update error");
				}
			}else{
				if($m->add($tmpData)){
					$this->success("add success");
				}else{
					$this->error("add error");
				}
			}
		}
	}
	
	/**
	 * 删除数据
	 */
	public function del(){
		$this->_del(M('Jsons'), I('ids'));
	}
}